<?php
class Comments_Model extends CI_Model {
	
	/**
	 * Enter description here ...
	 * @param unknown_type $options
	 */
	public function getItemComments( $item_id, $offset, $limit) {
		
		if (! is_int($item_id) || ! is_int($offset) || ! is_int($limit))
			return false;
		
		$this->load->database ();
		
		$this->db->where ( 'ITEMID', $item_id );
		$this->db->order_by ( 'CREATETIME', 'ASC' );
		$this->db->limit ( $limit, $offset );
		$this->db->from ( 'COMMENTS' );
		$query = $this->db->get ();
		
		$this->db->close ();
		
		print_r ( $this->db->last_query () );
		print_r ( $query->result_array () );
		
		return $query->result_array ();
	}
	
	public function getCommentsToUser( $user_id, $offset, $limit ) {
		
		if (! is_string($user_id) || ! is_int($offset) || ! is_int($limit))
			return false;
		
		$this->load->database ();
		
		$this->db->where ( 'TOUSERID', $user_id );
		$this->db->order_by ( 'CREATETIME', 'DESC' );
		$this->db->limit ( $limit, $offset );
		$this->db->from ( 'COMMENTS' );
		$query = $this->db->get ();
		
		$this->db->close ();
		
		print_r ( $this->db->last_query () );
		print_r ( $query->result_array () );
		
		return $query->result_array ();
	}
	
	public function getCommentsByUser($user_id, $offset, $limit) {
		if (! is_string($user_id) || ! is_int($offset) || ! is_int($limit))
			return false;
		
		$this->load->database ();
		
		$this->db->where ( 'USERID', $user_id );
		$this->db->order_by ( 'CREATETIME', 'DESC' );
		$this->db->limit ( $limit, $offset );
		$this->db->from ( 'COMMENTS' );
		$query = $this->db->get ();
		
		$this->db->close ();
		
		print_r ( $this->db->last_query () );
		print_r ( $query->result_array () );
		
		return $query->result_array ();
	}
	
	public function addComment($data = array()) {
		
		if (! $this->_required ( array ('ITEMID', 'USERID', 'TOUSERID', 'CONTENT' ), $data ))
			return false;
		
		$data ['CREATETIME'] = date ( 'Y-m-d H:i:s' );
		
		$this->load->database ();
		$this->db->insert ( 'COMMENTS', $data );
		$this->db->close ();
	}
}